package cn.stu.projectsummit.admin.service.impl;

import cn.stu.projectsummit.admin.mapper.AdminMapper;
import cn.stu.projectsummit.admin.pojo.dto.AdminLoginParam;
import cn.stu.projectsummit.admin.pojo.vo.AdminVO;
import cn.stu.projectsummit.admin.service.AdminService;


import cn.stu.projectsummit.base.exception.ServiceException;
import cn.stu.projectsummit.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    @Override
    public AdminVO login(AdminLoginParam adminLoginParam) {
        log.debug("登录业务,参数:{}",adminLoginParam);
        AdminVO adminVO = adminMapper.selectByAdminName(adminLoginParam.getUsername());
        //根据用户输入的用户名,查不到用户信息:用户名错误
        if(adminVO==null){
            //业务层不能直接响应结果给前端,业务层遇到问题需要抛出业务层异常!
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        //根据用户名查到的正确密码与用户输入的密码不一致:密码错误
        if(!adminVO.getPassword().equals(adminLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        //如果没有抛出任何异常,说明用户名与密码均正确,登录成功!
        return adminVO;
    }
}
